﻿using FruitCake.ContactPage.Models;
using Orchard.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FruitCake.ContactPage.ViewModels;
using Orchard;
using Orchard.Localization;

namespace FruitCake.ContactPage.Services
{
    public class ContactService : IContactService
    {
        public Localizer T { get; set; }
        private readonly IOrchardServices _services;        
        IRepository<ContactRequestRecord> _requestRepository;

        public ContactService(
            IOrchardServices orchardServices,
            IRepository<ContactRequestRecord> requestRepository)
        {
            _services = orchardServices;
            _requestRepository = requestRepository;
        }

        public int GetRequestCount()
        {
            return _requestRepository.Table.Count();            
        }
 
        public IEnumerable<ContactRequestRecord> GetRequests(
            int pageIndex, 
            int pageSize)
        {
            return _requestRepository.Table
                .OrderByDescending(r => r.RequestDate)
                .Skip((pageIndex-1) * pageSize)
                .Take(pageSize)
                .ToList();
        }
        
        public ContactRequestRecord GetRequest(int id)
        {
            return _requestRepository.Get(x => x.Id == id);
        }

        public int SaveRequest(ContactRequestRecord request)
        {
            if (request.Id <= 0)
                _requestRepository.Create(request);
            else
                _requestRepository.Update(request);
            return request.Id;
        }

        public void DoRequestAction(
            ViewModels.RequestAdminAction action, 
            int id)
        {
            DoRequestAction(action, new int[] { id });
        }

        public void DoRequestAction(
            ViewModels.RequestAdminAction action, 
            IEnumerable<int> itemIds)
        {
            if(itemIds == null) return;
            if(itemIds.Count() == 0) return;

            switch (action)
            {
                case (RequestAdminAction.None):
                    break;
                case (RequestAdminAction.Delete):
                    foreach (int id in itemIds) DeleteRequest(id);
                    break;
                default:
                    foreach (int id in itemIds) ChangeRequestState(id, (RequestState) action);
                    break;
            }
        }

        public void DeleteRequest(int id)
        {
            ContactRequestRecord request = GetRequest(id);
            if (request == null) return;
            _requestRepository.Delete(request);
        }

        public void ChangeRequestState(
            int id, 
            RequestState newState)
        {
            ContactRequestRecord request = GetRequest(id);
            if (request == null) return;

            request.StateValue = newState;
            request.StateDate = DateTime.Now;
            request.StateUser = _services.WorkContext.CurrentUser.UserName;

            _requestRepository.Update(request);
        }


        public IEnumerable<ReasonRule> GetDefaultReasons()
        {
            return new List<ReasonRule>
            {
                ReasonRule.Create(0, T("Information request").ToString()),
                ReasonRule.Create(1, T("Error reporting").ToString())                                
            };
        }
    }
}